//app.js
App({
  onLaunch: function () {
    wx.cloud.init({
      env:"prod-ayrkn"
    })
  },
  globalData: {
    userInfo: null,
    url:'https://www.meijianzj.com/',
    appid:'wxa9a85bd3c816ce3f',
    hasLogin: false,
    token: null,
    uid: null,
    fid: null,
    cartItems:null,
    hasUserInfo: false,
    rawData:''
  },
  getUserToken: function (callback) {
    var self = this
 
    self.globalData.token = self.globalData.token || wx.getStorageSync('token') || null
    self.globalData.rawData = self.globalData.rawData|| wx.getStorageSync('rawData') || null
    self.globalData.uid = self.globalData.uid|| wx.getStorageSync('user_id') || null
    if (self.globalData.token ) {
      callback(null, self.globalData.token)
    } else {
      wx.login({
        success: function (info) {
          wx.request({
            url: self.globalData.url +'index.php?s=api/user/login',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              code: info.code,
              app_id: 10001,
              user_info:self.globalData.rawData
            },
            method:'post',
            success: function (resToken) {
              console.log(resToken);
              if (resToken.data.code == 1) {
                self.globalData.token = resToken.data.data.token

                wx.setStorageSync('token', self.globalData.token)
              } else {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              }
            },
            fail: function (err) {
              console.log('拉取用户token失败，将无法正常使用开放接口等服务', err)
              callback(err)
            }
          })
        },
        fail: function (err) {
          console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
          callback(err)
        }
      })
    }
  },

})
